<?php
class Jadwal_jam_belajar extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('jadwal_model');
		$this->load->model('pelajaran_model');
	}
	
	function index($act='')
	{
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['JAM_KE'] = array('Jam Ke',100,TRUE,'center',1);
		$colModel['WAKTU'] = array('Waktu',400,TRUE,'center',1);
		$colModel['KETERANGAN'] = array('Keterangan',300,TRUE,'center',1);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Jadwal Jam Belajar'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		// if($data['value_semester']!=null and $data['value_tingkat']!=null and $data['value_tingkat']!=null)
			$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		// $buttons[] = array('Back to Pelajaran','back','org_js');
		
		//Build js
		$grid_js = build_grid_js('flex1',site_url("/jadwal_jam_belajar/listjadwal_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					 location.href='".base_url()."index.php/jadwal_jam_belajar/input_jadwalbelajar/'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/jadwal_jam_belajar/delete_jadwalbelajar')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
		
		// $data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		// $data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		// $data['semester'] = $this->pelajaran_model->getSmt($this->session->userdata('unitid'));
		// $data['url_filter'] = 'jadwal_jam_belajar/index/act_filter';
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listjadwal_grid()
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('JAM_KE','JAM_MULAI','JAM_SELESAI','KETERANGAN');
		
		$this->flexigrid->validate_post('ID_JJB','asc',$valid_fields);
		
		$records = $this->jadwal_model->get_jadwalbelajar($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'));
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$jam_mulai = split(':',$row->JAM_MULAI);
			$jam_selesai = split(':',$row->JAM_SELESAI);
			
			$record_items[] = array($row->ID_JJB,
			$no+1,
			'<div align="center">'.$row->JAM_KE .'</div>',
			'<div align="center">'.$jam_mulai[0].':'.$jam_mulai[1].' - '.$jam_selesai[0].':'.$jam_selesai[1].'</div>',
			'<div align="center">'.$row->KETERANGAN .'</div>',
			'<a href=\''.site_url().'/jadwal_jam_belajar/edit_jadwalbelajar/'.$row->ID_JJB.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('jamke', 'Jam Ke', 'required|numeric|callback_cekinputan');
			$this->form_validation->set_rules('jam_mulai', 'Jam Mulai', 'required|numeric');
			$this->form_validation->set_rules('menit_mulai', 'Menit Mulai', 'required|numeric');
			$this->form_validation->set_rules('jam_selesai', 'Jam Selesai', 'required|numeric');
			$this->form_validation->set_rules('menit_selesai', 'Menit Selesai', 'required|numeric');
			if($this->session->userdata('roleid')=='0') //diknas
				$this->form_validation->set_rules('nama_sekolah', 'Nama Sekolah', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('numeric', 'Kolom %s harus angka!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function cek_jadwal($kode_mp,$id_sekolah,$thn_ajaran,$kode_ujian){
		$rs_cek = $this->pelajaran_model->validasi_jenisujian($kode_mp,$id_sekolah,$thn_ajaran, $kode_ujian)->row();
		return $rs_cek;
	}
	
	function cekinputan($value){
		$jamke = $this->input->post('jamke');
		$jamke_asli = $this->input->post('jamke_asli');
		$jammulai = $this->input->post('jam_mulai');
		$menitmulai = $this->input->post('menit_mulai');
		$jamselesai = $this->input->post('jam_selesai');
		$menitselesai = $this->input->post('menit_selesai');
		$jum_awal = ($jammulai*60) + $menitmulai;
		$jum_akhir = ($jamselesai*60) + $menitselesai;
		
		$rs_waktu = $this->jadwal_model->cek_jjb($jamke)->row();
		
		if($rs_waktu->JUMLAH > 0 and $jamke!=$jamke_asli){
			$this->form_validation->set_message('cekinputan', ' Urutan jam ke '.$jamke.' sudah digunakan!.');
			return FALSE;
		}else{
			if($jamke == $jamke_asli){
				$rs_waktu_akhir = $this->jadwal_model->cek_waktu_akhir($jamke)->row();
				$jum_sebelumnya = ($rs_waktu_akhir->jamselesai * 60) + $rs_waktu_akhir->menitselesai;
				
				if($jum_awal<$jum_sebelumnya){
					$this->form_validation->set_message('cekinputan', ' Jam mulai tidak boleh berpotongan dengan jam akhir urutan sebelumnya!.');
					return FALSE;
				}
				else if ($jammulai > 23){
					$this->form_validation->set_message('cekinputan', ' Bilangan jam mulai berkisar dari 00 sampai 23.');
					return FALSE;
				}
				else if ($menitmulai > 59){
					$this->form_validation->set_message('cekinputan', ' Bilangan menit mulai berkisar dari 00 sampai 59.');
					return FALSE;
				}
				else if ($jamselesai > 23){
					$this->form_validation->set_message('cekinputan', ' Bilangan jam selesai berkisar dari 00 sampai 23.');
					return FALSE;
				}
				else if ($menitselesai > 59){
					$this->form_validation->set_message('cekinputan', ' Bilangan menit selesai berkisar dari 00 sampai 59.');
					return FALSE;
				}
				else if($jum_awal >= $jum_akhir){
					$this->form_validation->set_message('cekinputan', ' Waktu mulai tidak boleh lebih besar atau sama dengan waktu selesai.');
					return FALSE;
				}
				else
					return TRUE;
					
			}else{ //jika jam ke berubah
				$rs_waktu_akhir = $this->jadwal_model->cek_waktu_akhir($jamke)->row();
				$jum_sebelumnya = ($rs_waktu_akhir->jamselesai * 60) + $rs_waktu_akhir->menitselesai;
				
				$rs_waktu_mulai = $this->jadwal_model->cek_waktu_mulai($jamke)->row();
				if(!empty($rs_waktu_mulai))
					$jum_sesudahnya = ($rs_waktu_mulai->jammulai * 60) + $rs_waktu_mulai->menitmulai;
				
				if($jum_awal<$jum_sebelumnya){
					$this->form_validation->set_message('cekinputan', ' Jam mulai tidak boleh berpotongan dengan jam akhir urutan sebelumnya!.');
					return FALSE;
				}else{
					if(!empty($rs_waktu_mulai)){
						if($jum_akhir > $jum_sesudahnya){
							$this->form_validation->set_message('cekinputan', ' Jam selesai tidak boleh berpotongan dengan jam awal urutan selanjutnya!.');
							return FALSE;
						}
					}
					else if ($jammulai > 23){
						$this->form_validation->set_message('cekinputan', ' Bilangan jam mulai berkisar dari 00 sampai 23.');
						return FALSE;
					}
					else if ($menitmulai > 59){
						$this->form_validation->set_message('cekinputan', ' Bilangan menit mulai berkisar dari 00 sampai 59.');
						return FALSE;
					}
					else if ($jamselesai > 23){
						$this->form_validation->set_message('cekinputan', ' Bilangan jam selesai berkisar dari 00 sampai 23.');
						return FALSE;
					}
					else if ($menitselesai > 59){
						$this->form_validation->set_message('cekinputan', ' Bilangan menit selesai berkisar dari 00 sampai 59.');
						return FALSE;
					}
					else if($jum_awal >= $jum_akhir){
						$this->form_validation->set_message('cekinputan', ' Waktu mulai tidak boleh lebih besar atau sama dengan waktu selesai.');
						return FALSE;
					}
					// else if($rs_waktu->JUMLAH > 0){
						// $this->form_validation->set_message('cekinputan', ' Urutan jam ke '.$jamke.' sudah digunakan!.');
						// return FALSE;
					// }
					else
						return TRUE;
				}
			}
		}
	}
	
	function input_jadwalbelajar()
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['content'] = $this->load->view('form_jadwaljambelajar',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_jadwalbelajar();
		}            
    }
	
	function insert_jadwalbelajar(){
		if($this->input->post('jam_mulai')<10)
			$jammulai = '0'.$this->input->post('jam_mulai');
		else $jammulai = $this->input->post('jam_mulai');
		
		if($this->input->post('menit_mulai') < 10)
			$menitmulai = '0'.$this->input->post('menit_mulai');
		else $menitmulai = $this->input->post('menit_mulai');
		
		if($this->input->post('jam_selesai')<10)
			$jamselesai = '0'.$this->input->post('jam_selesai');
		else $jamselesai = $this->input->post('jam_selesai');
		
		if($this->input->post('menit_selesai') < 10)
			$menitselesai = '0'.$this->input->post('menit_selesai');
		else $menitselesai = $this->input->post('menit_selesai');
		
		$data = array(
			'TAHUN_AJARAN'			=> $this->session->userdata('tahunajaran'),
			'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
			'JAM_KE'				=> $this->input->post('jamke'),	
			'JAM_MULAI'				=> $jammulai.':'.$menitmulai,	
			'JAM_SELESAI'			=> $jamselesai.':'.$menitselesai,	
			// 'JAM_MULAI'				=> $this->input->post('jam_mulai').':'.$this->input->post('menit_mulai'),	
			// 'JAM_SELESAI'			=> $this->input->post('jam_selesai').':'.$this->input->post('menit_selesai'),	
			'KETERANGAN'			=> $this->input->post('keterangan')
		);
		$this->jadwal_model->insert_jadwalbelajar($data);
		
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('jadwal_jam_belajar'); 
	}
	
	function edit_jadwalbelajar($id_jjb)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['rs_jadwalbelajar'] = $this->jadwal_model->get_jadwalID($id_jjb);
			$data['content'] = $this->load->view('form_jadwaljambelajar',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->update_jadwalbelajar($id_jjb);
		}            
    }
	
	function update_jadwalbelajar($id_jjb){
		if($this->input->post('jam_mulai')<10)
			$jammulai = '0'.$this->input->post('jam_mulai');
		else $jammulai = $this->input->post('jam_mulai');
		
		if($this->input->post('menit_mulai') < 10)
			$menitmulai = '0'.$this->input->post('menit_mulai');
		else $menitmulai = $this->input->post('menit_mulai');
		
		if($this->input->post('jam_selesai')<10)
			$jamselesai = '0'.$this->input->post('jam_selesai');
		else $jamselesai = $this->input->post('jam_selesai');
		
		if($this->input->post('menit_selesai') < 10)
			$menitselesai = '0'.$this->input->post('menit_selesai');
		else $menitselesai = $this->input->post('menit_selesai');
		
		$data = array(
			'TAHUN_AJARAN'			=> $this->session->userdata('tahunajaran'),
			'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
			'JAM_KE'				=> $this->input->post('jamke'),	
			'JAM_MULAI'				=> $jammulai.':'.$menitmulai,	
			'JAM_SELESAI'			=> $jamselesai.':'.$menitselesai,		
			'KETERANGAN'			=> $this->input->post('keterangan')
		);
		$this->jadwal_model->update_jadwalbelajar($data, $id_jjb);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('jadwal_jam_belajar'); 
	}
	
	//Delete
	function delete_jadwalbelajar()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->jadwal_model->delete_jadwalbelajar($ta_id);
				$msg .= "Data Jadwal Jam Belajar berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
	
}
?>
